<?php

include_once(dirname(__FILE__) . "/../config.php");

class db_task extends database_table {
    const STATUS_OPEN = 0;
    const STATUS_PARSED = 1;
    const STATUS_PENDING = 2;
    const STATUS_ERROR = 3;

    private static $instance = null;
    public static function inst() {
        if (self::$instance == null)
            self::$instance = new db_task();
        return self::$instance;
    }

    private function db_task() {
        parent::database_table(MYSQL_DATABASE, MYSQL_PREFIX . "task");
    }

    public function get($id) {
        $id = (int)$id;
        return $this->get_one("id = $id");
    }

    public function all() {
        return $this->get_all("", "ORDER BY id DESC");
    }

    public function allParser($pid) {
        $pid = (int)$pid;
        return $this->get_all("parser = $pid ORDER BY id DESC");
    }

    public function add($parser, $owner, $target, $title, $comments) {
        $now = time(null);
        return $this->insert(array("parser" => $parser, "owner" => $owner, "target" => $target, "title" => $title, "comments" => $comments, "create_time" => $now));
    }

    // public function modify($id, $name) {
    //     $id = (int)$id;
    //     return $this->update(array("name" => $name), "id = $id");
    // }

    public function parsed($id) {
        $id = (int)$id;
        return $this->update(array("stat" => self::STATUS_PARSED), "id = $id");
    }

    public function error($id) {
        $id = (int)$id;
        return $this->update(array("stat" => self::STATUS_ERROR), "id = $id");
    }


    public function update_result($id, $result) {
        $id = (int)$id;
        $now = time(null);
        return $this->update(array("stat" => self::STATUS_PARSED, "result" => $result, "result_time" => $now), "id = $id");
    }
};


